package com.zhiyou.app.shop.manager.dao.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.zhiyou.app.shop.common.util.DateUtil;
import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.manager.action.vo.OrderVo;
import com.zhiyou.app.shop.manager.dao.IGoodsOrderDao;
import com.zhiyou.app.shop.manager.dao.pojo.GoodsOrder;
import com.zhiyou.app.shop.manager.dao.pojo.OrderDetail;
@Repository
public class GoodsOrderDaoImpl extends BaseHibernateDAO<GoodsOrder, String> implements
		IGoodsOrderDao {

	public GoodsOrderDaoImpl() {
		super(GoodsOrder.class);
	}

	public void findGoodsOrderByExample(Page page, GoodsOrder go) {
		DetachedCriteria dc = DetachedCriteria.forClass(GoodsOrder.class);
		if(null != go.getUserDetail()){
			dc.add(Restrictions.eq("userDetail.id", go.getUserDetail().getId()));
		}
		if(null != go.getIsPay()){
			dc.add(Restrictions.eq("isPay", go.getIsPay()));
		}
		this.findByCriteria(page, dc, Order.desc("createDate"));
	}

	public void findMyShopOrder(Page page, GoodsOrder go) {
		DetachedCriteria dc = DetachedCriteria.forClass(GoodsOrder.class);
		dc.createAlias("payOrder", "payOrder");
		if(null != go.getIsPay()){//是否付款
			dc.add(Restrictions.eq("isPay",go.getIsPay()));
		}
		if(null != go.getIsDelivery()){//是否发货
			dc.add(Restrictions.eq("isDelivery",go.getIsDelivery()));
		}
		if(null != go.getIsReceipt()){//是否收货
			dc.add(Restrictions.eq("isReceipt",go.getIsReceipt()));
		}
		if(null != go.getShop()){
			dc.add(Restrictions.eq("shop.id",go.getShop().getId()));
		}
		this.findByCriteria(page, dc, Order.desc("createDate"));
		
		List<Object[]> list = page.getResult();
		List<GoodsOrder> goodsOrderList = new ArrayList<GoodsOrder>(list.size());
		if(! list.isEmpty()){
			for(Object[] objs : list){
				GoodsOrder order = (GoodsOrder)objs[1];
				Set<OrderDetail> ds = order.getOrderDetails();
				Iterator<OrderDetail> it = ds.iterator();
				goodsOrderList.add(order);
			}
		}
		page.setResult(goodsOrderList);
	}

	public void findMyOrder(Page page, GoodsOrder go) {
		DetachedCriteria dc = DetachedCriteria.forClass(GoodsOrder.class);
		dc.createAlias("shop", "shop");
		if(null != go.getIsPay()){//是否付款
			dc.add(Restrictions.eq("isPay",go.getIsPay()));
		}
		if(null != go.getIsDelivery()){//是否发货
			dc.add(Restrictions.eq("isDelivery",go.getIsDelivery()));
		}
		if(null != go.getIsReceipt()){//是否收货
			dc.add(Restrictions.eq("isReceipt",go.getIsReceipt()));
		}
		if(null != go.getUserDetail()){
			dc.add(Restrictions.eq("userDetail.id",go.getUserDetail().getId()));
		}
		this.findByCriteria(page, dc, Order.desc("createDate"));
		
		List<Object[]> list = page.getResult();
		List<GoodsOrder> goodsOrderList = new ArrayList<GoodsOrder>(list.size());
		if(! list.isEmpty()){
			for(Object[] objs : list){
				GoodsOrder order = (GoodsOrder)objs[1];
				Set<OrderDetail> ds = order.getOrderDetails();
				Iterator<OrderDetail> it = ds.iterator();
				goodsOrderList.add(order);
			}
		}
		page.setResult(goodsOrderList);
	}

	public void findAllShopOrders(Page page, OrderVo vo) {
		String baseHql = " from GoodsOrder go left join go.shop s " +
				" where go.isReceipt=true ";
		Map<String, Object> params = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(vo.getShopName())){
			baseHql += " and s.name like :shopName ";
			params.put("shopName", "%"+vo.getShopName()+"%");
		}
		if(StringUtils.isNotBlank(vo.getBeginDate())){
			baseHql += " and go.createDate>:beginDate ";
			Date date = DateUtil.getBeginDay(vo.getBeginDate());
			params.put("beginDate", date);
		}
		if(StringUtils.isNotBlank(vo.getEndDate())){
			baseHql += " and go.createDate<:endDateTime ";
			Timestamp endTime = new Timestamp(DateUtil.getEndDay(vo.getEndDate()).getTime());
			params.put("endDateTime", endTime);
		}
		baseHql += "group by s.id, s.name, s.imgPath ";
		String countHql = "select count(s.id)"+baseHql;
		Query countQuery = this.getSession().createQuery(countHql);
		this.setQueryParam(countQuery, params);
		Object obj = countQuery.uniqueResult();
		int totalCount = obj != null ? Integer.valueOf(obj.toString()) : 0;
		page.setTotalCount(totalCount);
		
		String listHql = "select new com.zhiyou.app.shop.manager.action.vo.OrderVo(" +
				"s.id, s.name, s.imgPath, count(s.id), sum(go.orderPrice), s.createDate)" +
				baseHql + " order by sum(go.orderPrice) desc";
		Query listQuery = this.getSession().createQuery(listHql);
		this.setQueryParam(listQuery, params);
		listQuery.setFirstResult(page.getOffset()).setMaxResults(page.getPageSize());
		List<OrderVo> list = listQuery.list();
		page.setResult(list);
	}

	private void setQueryParam(Query query, Map<String, Object> params) {
		for(String key : params.keySet()){
			query.setParameter(key, params.get(key));
		}
	}
	
	
	
}
